import extend from 'dva-model-extend'
import settings from '@/settings'
import model from '@/baseModel'
import { role, roles, modules } from '@/services/api'

const { pagination } = settings

export default extend(model, {
	namespace: 'role',
	state: {
		modules: [],
		authIds: []
	},
	effects: {
		*reset(_, {
			put
		}) {
			yield put({
				type: 'updateState',
				payload: {
					data: undefined,
					authIds: []
				}
			})
		},
		*fetchOne({
			payload
		}, {
			call,
			put
		}) {
			const { data = {} } = yield call(role, payload)
			const {
				authIds = [],
				...rest
			} = data
			yield put({
				type: 'updateState',
				payload: {
					data: rest,
					authIds
				}
			})
		},
		*fetchAll({
			payload = pagination
		}, {
			call,
			put
		}) {
			const { data } = yield call(roles, payload)
			yield put({
				type: 'updateState',
				payload: {
					list: data,
					params: payload
				}
			})
		},
		*fetchModules({
			payload
		}, {
			call,
			put
		}) {
			const { data } = yield call(modules, payload)
			yield put({
				type: 'updateState',
				payload: {
					modules: data
				}
			})
		}
	}
})